package com.fh.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class WebSocketExample {
    public static void main(String[] args) {
        String serverUrl = "ws://localhost:8888/websocket/fh"; // WebSocket服务器地址
        WebSocketClient client = null;

        try {
            client = new WebSocketClient(new URI(serverUrl)) {
                @Override
                public void onOpen(ServerHandshake serverHandshake) {
                    System.out.println("连接已经建立");
                }

                @Override
                public void onMessage(String message) {
                    Map<String,Object> map = JSON.parseObject(message, new TypeReference<Map<String,Object>>() {
                    }.getType());
                    String data = map.get("data")
                            .toString();
                    if("exit".equals(data)){
                        System.out.println("用户: " + map.get("userId").toString() + "已存在");
                        System.exit(1);
                    }
                    System.out.println("发送者: " + map.get("userId").toString());
                    System.out.println("消息: " + map.get("data").toString());
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    System.out.println("Connection closed.");
                }

                @Override
                public void onError(Exception e) {
                    e.printStackTrace();
                }
            };

            client.connect(); // 建立WebSocket连接
            WebSocketClient finalClient = client;
            Thread inputThread = new Thread(() -> {
                try {
                    Thread.sleep(1000);
                } catch(InterruptedException e) {
                    throw new RuntimeException(e);
                }
                Scanner scanner = new Scanner(System.in);
                HashMap<String,Object> map = new HashMap<>();
                System.out.print("请输入你的用户名: ");
                String name = scanner.next();
                map.put("userId",name);
                while (true) {
                    System.out.println("请输入接收方的ID(如果有多个请用,分开): ");
                    String next = scanner.next();
                    String[] split = next.split(",");
                    map.put("targetIds",split);
                    System.out.println("请输入消息: ");
                    String data = scanner.next();
                    if("exit".equals(data)){
                        System.exit(1);
                    }
                    map.put("data",data);
                    finalClient.send(JSON.toJSONString(map)); // 发送消息
                }
            });

            inputThread.start(); // 启动输入线程

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}